//package com.service.scheduling.task;
//
//import com.google.common.collect.Lists;
//import com.service.dao.constants.ConfigConstants;
//import com.service.dao.enums.CoinsEnum;
//import com.service.dao.enums.HashRateOrderEnums;
//import com.service.dao.enums.MemberJAType;
//import com.service.dao.enums.MinerStatusEnum;
//import com.service.dao.model.OrderProductHrCombo;
//import com.service.dao.model.OrderProductHrComboDetails;
//import com.service.dao.model.ProductHrCombo;
//import com.service.scheduling.dto.MemberStoreChange;
//import com.service.scheduling.service.ConfigService;
//import com.service.scheduling.service.MemberStoreService;
//import com.service.scheduling.service.OrderProductHrComboService;
//import com.service.scheduling.service.ProductHrComboService;
//import com.service.scheduling.support.MemberStoreLockUtils;
//import com.service.scheduling.utils.DateUtil;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang3.RandomUtils;
//import org.apache.commons.lang3.time.DateFormatUtils;
//import org.apache.commons.lang3.time.DateUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Component;
//import org.springframework.util.ObjectUtils;
//
//import java.math.BigDecimal;
//import java.util.Date;
//import java.util.List;
//import java.util.Map;
//
///**
// * <dl>
// * <dt>back-end</dt>
// * <dd>Description:</dd>
// * <dd>Copyright: Copyright (C) 2015</dd>
// * <dd>Company: </dd>
// * <dd>CreateDate: 2019年12月15日</dd>
// * </dl>
// *
// * @author
// */
//@Slf4j
//@Component
//public class HrComboETHMiningTask {
//
//    @Autowired
//    private MemberStoreLockUtils memberStoreLockUtils;
//
//    @Autowired
//    private MemberStoreService memberStoreService;
//
//    @Autowired
//    private ProductHrComboService productHrComboService;
//
//    @Autowired
//    private OrderProductHrComboService orderProductHrComboService;
//    @Autowired
//    private ConfigService configService;
//
//    private static BigDecimal baseNum = new BigDecimal(100L);
//
//
////    @Scheduled(cron = "0 20 15 * * ?")
//    public void ETHRateMining() {
//        log.info("ETH购买算力定时任务-开始");
//        String minerStatus = configService.getByConfigName(ConfigConstants.MINER_STATUS).getConfValue();
//        if(MinerStatusEnum.OFF.equals(minerStatus)){
//            log.info("矿机暂停运行......");
//            return;
//        }
//        Date currentDate = new Date();
//        String pattern = "yyyy-MM-dd";
//        String currentDateStr = DateFormatUtils.format(currentDate, pattern);
//        Date yesterday = DateUtils.addDays(currentDate, -1);
//        List<OrderProductHrCombo> hrComboList = orderProductHrComboService.listMiningOrder(CoinsEnum.ETH.getCode(), yesterday);
//        if (ObjectUtils.isEmpty(hrComboList)) {
//            log.info("暂无ETH算力套餐订单");
//            return;
//        }
//        Map<Integer, ProductHrCombo> productHrComboMap = productHrComboService.listHrCombo2Map();
//        String yesterdayStr = DateFormatUtils.format(yesterday, pattern);
//        List<Integer> endList = Lists.newArrayList();
//        hrComboList.parallelStream().forEach(item -> {
//            //已支付且未挖矿的订单第二天凌晨开始挖矿
//            if (ObjectUtils.nullSafeEquals(item.getOrderStatus(), HashRateOrderEnums.Status.payed.getCode())
//                    && item.getStartTime().before(yesterday)) {
//                log.info("算力套餐订单<orderId:{}>还未开始", item.getId());
//                return;
//            }
//            if (currentDate.after(item.getEndTime())) {
//                log.info("算力套餐订单<orderId:{}>已结束", item.getId());
//                endList.add(item.getId());
//                return;
//            }
//            if (DateUtils.isSameDay(currentDate, item.getEndTime())) {
//                log.info("算力套餐订单<orderId:{}>结算最后一天<{}>", item.getId(), currentDate);
//                endList.add(item.getId());
//            }
//            OrderProductHrComboDetails detail = orderProductHrComboService.getHrComboDetail(item.getId());
//            if (!ObjectUtils.isEmpty(memberStoreService.getMiningRecord(item.getMemberId(), detail.getId(), currentDate))) {
//                log.info("算力套餐订单<orderId:{},orderDetailId:{}>,今天<{}>已发放挖矿所得",
//                        detail.getHrOrderId(), detail.getId(), currentDateStr);
//                return;
//            }
//            BigDecimal miningNum = getHrComboRateMining(detail, productHrComboMap);
//            log.info("算力套餐订单<orderId:{},orderDetailId:{}>今天<{}>挖矿所得{}", detail.getHrOrderId(), detail.getId(), currentDateStr, miningNum);
//            boolean isLock = memberStoreLockUtils.lock(item.getMemberId(), detail.getCoinsId());
//            if (!isLock) {
//                return;
//            }
//            if (miningNum.compareTo(BigDecimal.ZERO) <= 0) {
//                return;
//            }
//            memberStoreService.changeMemberStore(
//                    MemberStoreChange.builder()
//                            .memberId(item.getMemberId())
//                            .coinsId(detail.getCoinsId())
//                            .coinsName(detail.getCoinsName())
//                            .jaType(MemberJAType.MINING)
//                            .orderId(item.getId())
//                            .orderDetailId(detail.getId())
//                            .changeNum(miningNum)
//                            .remark(yesterdayStr + MemberJAType.MINING.getMsg())
//                            .build());
//            memberStoreLockUtils.unlock(item.getMemberId(), detail.getCoinsId());
//            OrderProductHrCombo miningCombo = new OrderProductHrCombo();
//            miningCombo.setId(item.getId());
//            miningCombo.setAccumulatedIncome(item.getAccumulatedIncome().add(miningNum));
//            miningCombo.setOrderStatus(HashRateOrderEnums.Status.payed_effective.getCode());
//            orderProductHrComboService.updateById(miningCombo);
//        });
//        //修改过期算力订单
//        orderProductHrComboService.updateHrComboRateOrder2Complete(endList);
//        log.info("ETH购买算力定时任务-结束");
//    }
//
//    public BigDecimal getHrComboRateMining(OrderProductHrComboDetails orderDetail, Map<Integer, ProductHrCombo> productHrComboMap) {
//        BigDecimal flexedNum = orderDetail.getFlexedNum();
//        BigDecimal floatingNum = orderDetail.getFloatingNum();
//        if(productHrComboMap.containsKey(orderDetail.getComboId())){
//            flexedNum  = productHrComboMap.get(orderDetail.getComboId()).getFlexedNum();
//            floatingNum  = productHrComboMap.get(orderDetail.getComboId()).getFloatingNum();
//        }
//
//        BigDecimal singleMiningNum = flexedNum.add(getFloatNum(floatingNum));
//        BigDecimal totalNum = orderDetail.getComputingPower().multiply(BigDecimal.valueOf(orderDetail.getOrderNum()));
//        return singleMiningNum.multiply(totalNum);
//    }
//
//    private BigDecimal getFloatNum(BigDecimal floatNum) {
//        return BigDecimal.valueOf(RandomUtils.nextDouble(0, floatNum.doubleValue()));
//    }
//
//    /**
//     * 更新今日订单状态
//     */
////    @Scheduled(cron = "0 22 15 * * ?")
//    public void updateTodayHrRate() {
//        log.info("更新今日开挖算力状态-开始");
//        Date currentDate = new Date();
//        Date today = DateUtil.getDayOfStart(currentDate);
//        List<OrderProductHrCombo> hrComboList = orderProductHrComboService.listMiningOrder(CoinsEnum.ETH.getCode(), today);
//        if (ObjectUtils.isEmpty(hrComboList)) {
//            log.info("暂无算力套餐订单");
//            return;
//        }
//        hrComboList.parallelStream().forEach(item -> {
//            if(item.getOrderStatus().equals(HashRateOrderEnums.Status.payed.getCode())){
//                //更新今日已付款，开挖的订单状态
//                OrderProductHrCombo miningCombo = new OrderProductHrCombo();
//                miningCombo.setId(item.getId());
//                miningCombo.setOrderStatus(HashRateOrderEnums.Status.payed_effective.getCode());
//                orderProductHrComboService.updateById(miningCombo);
//            }
//        });
//        log.info("更新今日开挖算力状态-结束");
//    }
//}
